package demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.GregorianCalendar;

/**
 * @author : GuoFei
 * @date : 2019/4/29 15:25
 */

public class Counter {
    public void reset() {
        count = 0;
    }

    public void increment() {
        count += 1;
    }

    public int getValue() {
        return count;
    }

    private int count = 0;
}

class Mini {
    public void check(long time) {
        if (time < miniTime)
            miniTime = time;
    }

    public long getValue() {
        return miniTime;
    }

    public void reset() {
        miniTime = 92233720368547L;
    }

    private long miniTime = 92233720368547L;
}

class Watch {
    public void start() {
        GregorianCalendar now = new GregorianCalendar();
        startTime = now.getTimeInMillis();
    }

    public void showElapsedTime() {
        GregorianCalendar now = new GregorianCalendar();
        long currentTime = now.getTimeInMillis();
        long difference = (currentTime - startTime) / 1000;
        System.out.print(difference);
        System.out.println("seconds hava elapsed.");
    }

    public long getElapsedTime() {
        GregorianCalendar now = new GregorianCalendar();
        long currentTime = now.getTimeInMillis();
        return (currentTime - startTime);
    }

    private long startTime;
}

class TypingTutor {
    public long test() throws Exception {
        BufferedReader keyboard = new BufferedReader(
                new InputStreamReader(System.in));          //只有是用到输入输出流，就必须进行throws Exception
        long testTime;
        Watch sw = new Watch();
        System.out.println("Hit Enter to start!,practiceString:" + practiceString);
        String inputString = keyboard.readLine();
        System.out.println("Type!");
        sw.start();
        inputString = keyboard.readLine();
        testTime = sw.getElapsedTime();
        if (practiceString.equals(inputString)) {
            prefectCount.increment();
            return testTime;
        } else return -1;
    }

    public long getLowestTime() {
        return lowestTime.getValue();
    }

    public long getNumPrefect() {
        return prefectCount.getValue();
    }

    public void setPractieString(String newString) {
        practiceString = newString;
        prefectCount.reset();
        lowestTime.reset();
    }

    private String practiceString = "The quick brown fox jumps over the lazy dog.";
    private Counter prefectCount = new Counter();
    private Mini lowestTime = new Mini();


    public static void main(String[] arg) throws Exception {     //即使是调用的函数里面有buffer，也要throws Exception
        TypingTutor t = new TypingTutor();
        System.out.println("First test:");
        long result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        System.out.println("Second test:");
        result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        System.out.println("Third test:");
        result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        if (t.getNumPrefect() > 0) {
            System.out.println("Your lowestTime is: ");
            System.out.println(t.getLowestTime());
        } else
            System.out.println("You failed in three times!");
        t.setPractieString("Exotic zebras jaywalk with impunity.");      //这里改变字符串值
        System.out.println("First test:");
        result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        System.out.println("Second test:");
        result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        System.out.println("Third test:");
        result = t.test();
        if (result > 0) {
            System.out.println("your time is: ");
            System.out.println(result / 1000 + "seconds");
        } else
            System.out.println("You did not type the correct number!");
        System.out.println();
        if (t.getNumPrefect() > 0) {
            System.out.println("Your lowestTime is: ");
            System.out.println(t.getLowestTime());
        } else
            System.out.println("You failed in three times!");

    }


}
